<template>
  <div class="box bgc3 sku-sale-rank">
    <!-- TODO: 分辨大怎么展示问UI -->
    <div class="header">
      <div class="title">
        本月热榜<span class="sub-title">{{ start }} ~ {{ end }}</span>
      </div>
    </div>
    <div class="body">
      <el-row v-for="(item, index) in skuSaleRank" :key="index">
        <el-col :span="5">
          <div :class="'top top' + (index + 1)">
            {{ index + 1 }}
          </div>
        </el-col>
        <el-col :span="13">
          <div class="sku-name" :title="item.playName">
            {{ item.playName }}
          </div>
        </el-col>
        <el-col :span="6">
          <div class="count">{{ item.total }}单</div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { getRanking } from "@/api/record/record";
import { ref, onMounted } from 'vue';
import dayjs from 'dayjs';

// 定义变量
const skuSaleRank = ref([]);

const start = dayjs().startOf('month').format('YYYY.MM.DD');
const end = dayjs().endOf('day').format('YYYY.MM.DD');

const data = reactive({
  query: {
    start: start,
    end: end
  }
});

// 定义方法
function getRankings() {
  getRanking(data.query).then(
    response => {
      if (response.code === 200) {
        // 添加调试信息
        console.log('Response data:', response.data);
        // 更新 skuSaleRank 数据
        skuSaleRank.value = response.data;
        // 再次添加调试信息
        console.log('Updated skuSaleRank:', skuSaleRank.value);
      }
    }
  ).catch(error => {
    console.error('Error fetching sales data:', error);
  });
};

// 在组件初始化时调用获取数据的方法
onMounted(() => {
  getRankings();
});
</script>

<style lang="scss" scoped>
@import '@/assets/styles/variables.module.scss';

.sku-sale-rank {
  display: flex;
  flex-direction: column;
  height: calc((100vh - 120px) * 0.6);
  min-height: 538px;
  background: #FFFFFF;
  border-radius: 20px;

  .body {
    flex: 1;
    display: flex;
    flex-direction: column;
    margin-top: 20px;

    .el-row {
      display: flex;
      align-items: center;
      margin-bottom: 10px; // 确保每行之间有间距
    }

    .top {
      display: inline-block;
      width: 16px;
      height: 20px;
      margin-left: 10px;
      background: url('@/assets/user-task-stats/top.png');
      text-align: center;
      font-size: 12px;
      font-weight: normal;
      color: #E9B499;
      line-height: 14px;
    }

    .top1 {
      width: 21px;
      height: 20px;
      background: url('@/assets/user-task-stats/top1.png');
      color: #8E5900;
    }

    .top2 {
      width: 21px;
      height: 20px;
      background: url('@/assets/user-task-stats/top2.png');
      color: #494949;
    }

    .top3 {
      width: 21px;
      height: 20px;
      background: url('@/assets/user-task-stats/top3.png');
      color: #CF6D3D;
    }

    .sku-name {
      height: 20px;
      font-size: 14px;
      font-weight: 500;
      color: $--color-text-primary;
      line-height: 20px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }

    .count {
      height: 20px;
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #737589;
      line-height: 20px;
      text-align: right;
    }
  }
}
</style>